<%--
  Created by IntelliJ IDEA.
  User: Fmc
  Date: 2022/6/23
  Time: 16:09
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>部门展示</title>
        <link rel="stylesheet" href="../../static/layui-main/dist/css/layui.css">
        <script src="../../static/layui-main/src/layui.js"></script>
    </head>
    <body>
    <!-- 搜索框 -->
    <form class="layui-form" action="" id="searchForm">
        <div class="layui-inline">
            <label class="layui-form-label">部门名称</label>
            <div class="layui-input-inline">
                <input type="text" id="deptName" name="deptName"  placeholder="部门名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">创建人</label>
            <div class="layui-input-inline">
                <input type="text" id="createBy" name="createBy"  placeholder="创建人" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">修改人</label>
            <div class="layui-input-inline">
                <input type="text" id="updateBy" name="updateBy"  placeholder="修改人" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <div class="layui-input-inline">
                <button class="layui-btn" lay-submit lay-filter="formDemo" id="reserch">查询</button>
                <button type="reset" class="layui-btn layui-btn-primary" id="search_reset">重置</button>
            </div>
        </div>
    </form>

    <!-- 头部工具栏 -->
    <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn layui-btn" lay-event="add">添加</button>
            <button class="layui-btn layui-btn-danger layui-btn" lay-event="delete">删除</button>
        </div>
    </script>

    <!-- 行内工具栏 -->
    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="update">修改</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</a>
    </script>

    <!-- 添加时，弹出的表单 -->
    <form class="layui-form" action="" id="addDeptForm" style="display: none">
        <div class="layui-form-item">
            <label class="layui-form-label">部门名称</label>
            <div class="layui-input-inline">
                <input type="text" name="deptName" required lay-verify="required" placeholder="请输入部门名称" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <input type="checkbox" checked="" name="state" lay-skin="switch" lay-filter="switchAdd" lay-text="正常|异常">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">创建人</label>
            <div class="layui-input-inline">
                <input type="text" name="createBy" required lay-verify="required" placeholder="请输入创建人" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="addDept">提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>

    <!-- 修改时，弹出的表单 -->
    <form class="layui-form" action="" lay-filter="updateDeptFilter" id="updateDeptForm" style="display: none">
        <div class="layui-form-item">
            <label class="layui-form-label">部门</label>
            <div class="layui-input-inline">
                <input type="text" name="deptId" disabled required lay-verify="required" readonly class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">部门名称</label>
            <div class="layui-input-inline">
                <input type="text" name="deptName" required lay-verify="required"  class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <input type="checkbox"  name="state" lay-skin="switch" lay-filter="switchUpdate" lay-text="正常|异常">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">创建人</label>
            <div class="layui-input-inline">
                <input type="text" name="createBy"  required lay-verify="required"  class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">创建时间</label>
            <div class="layui-input-inline">
                <input type="text" name="createTime" disabled id="date1" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">修改人</label>
            <div class="layui-input-inline">
                <input type="text" name="updateBy"  required lay-verify="required"  class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">修改时间</label>
            <div class="layui-input-inline">
                <input type="text" name="updateTime" disabled id="date2" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="updateDept">保存</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>

    <!-- table表格 -->
    <%--lay-filter相当于dom元素的id，layui专用的选择器--%>
    <table id="demo" lay-filter="test"></table>

    </body>
    <script>
        layui.use(['table','form','jquery','laydate'], function(){
            var table = layui.table;
            var form = layui.form;
            var $ = layui.jquery;
            var laydate = layui.laydate;

            //第一个实例,渲染数据
            table.render({
                elem: '#demo'
                ,height: 500
                ,url: '${pageContext.request.contextPath}/dept/selectAllDept.do'//数据接口
                ,method: 'post'
                ,page: false//关闭分页
                ,autoSort: false//禁用前端的自动排序，开启后端的服务器排序
                ,toolbar: '#toolbarDemo'
                ,cols: [[
                    //表头
                    {type: 'checkbox', fixed: 'left'}
                    ,{field: 'deptId', title: '部门编号', width:120, sort: true, fixed: 'left'}
                    ,{field: 'deptName', title: '部门名称', width:100}
                    ,{field: 'state', title: '状态', width:100}
                    ,{field: 'createBy', title: '创建人', width:100}
                    ,{field: 'createTime', title: '创建时间', width:150, sort: true}
                    ,{field: 'updateBy', title: '修改人', width:100}
                    ,{field: 'updateTime', title: '修改时间', width:150, sort: true}
                    ,{fixed: 'right', title: '操作', width: 150, align:'center', toolbar: '#barDemo'}
                ]]
            });

            //触发排序事件
            table.on('sort(test)', function(obj){ //注：sort 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值 "
                console.log(obj.field); //当前排序的字段名
                console.log(obj.type); //当前排序类型：desc（降序）、asc（升序）、null（空对象，默认排序）
                console.log(this); //当前排序的 th 对象

                //尽管我们的 table 自带排序功能，但并没有请求服务端。
                //有些时候，你可能需要根据当前排序的字段，重新向服务端发送请求，从而实现服务端排序，如：
                table.reload('demo', {
                    initSort: obj //记录初始排序，如果不设的话，将无法标记表头的排序状态。
                    ,type: 'post'
                    ,where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                        field: obj.field //排序字段
                        ,order: obj.type //排序方式
                    }
                    ,method: 'post'
                });
            });

            //模糊查询,监听提交
            form.on('submit(formDemo)', function(data){
                table.reload('demo',{
                    where: {
                        deptName: data.field.deptName
                        ,createBy: data.field.createBy
                        ,updateBy: data.field.updateBy
                    }
                    ,page: {
                        curr: 1
                    }
                });
                return false;
            });

            //重置按钮绑定单击事件
            $("#search_reset").click(function(){
                table.reload('demo',{
                    where: {
                        deptName: ""
                        ,createBy: ""
                        ,updateBy: ""
                    },
                    page: {
                        curr: 1
                    }
                });
            });

            //监听头部工具栏事件
            table.on('toolbar(test)', function (obj){
                //获取选中的数据
                var checkStatus = table.checkStatus(obj.config.id);
                var data = checkStatus.data;
                console.log(data);
                switch (obj.event) {
                    case 'add':
                        layer.open({
                            title: '添加部门'
                            ,type: 1
                            ,content:$('#addDeptForm')
                            ,area: ['500px','500px']
                        });
                        // 监听addDeptForm表单的提交事件
                        form.on('submit(addDept)',function (data){
                            $.ajax({
                                url: '${pageContext.request.contextPath}/dept/addDept.do'
                                ,data: data.field
                                ,dataType: "json"
                                ,type: "post"
                                ,success: function (redata) {
                                    if (redata.code == 0) {
                                        layer.msg(redata.msg);
                                        layer.closeAll();//关闭对话框
                                        table.reload('demo',{
                                            page: {
                                                curr: 1
                                            }
                                        });
                                    } else {
                                        layer.msg(redata.msg);
                                    }
                                }
                            });
                            return false;
                        });
                        break;
                    case  'delete':
                        if (data.length === 0) {
                            layer.msg('请选择一行');
                        } else {
                            //layer.msg('删除');
                            //定义一个数组，封装所有选中的id
                            var ids = [];
                            //循环所有选中的行
                            for (let i=0;i<data.length;i++) {
                                ids[i] = data[i].deptId;
                            }
                            layer.confirm('真的删除选中行吗？',function (index){
                                $.ajax({
                                    url: '${pageContext.request.contextPath}/dept/deleteDept.do'
                                    ,data: {deptId:ids.join(",")}
                                    ,dataType: "json"
                                    ,type: "post"
                                    ,success: function (redata) {
                                        if (redata.code == 0) {
                                            //关闭对话框
                                            layer.closeAll();
                                            //刷新表格数据
                                            table.reload('demo',{
                                                page: {
                                                    curr: 1
                                                }
                                            });
                                        }
                                        layer.msg(redata.msg);
                                    }
                                });
                            });
                        }
                        break;
                }
            });

            //监听行工具事件
            table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
                var data = obj.data //获得当前行数据
                var layEvent = obj.event; //获得 lay-event 对应的值
                // 回显状态开关
                if (data.state == "异常"){
                    $("#updateDeptForm input[type=checkbox]").prop("checked", "");
                } else {
                    $("#updateDeptForm input[type=checkbox]").prop("checked", "checked");
                }
                if (layEvent === 'update') {
                    layer.open({
                        title: '修改部门信息'
                        ,type: 1
                        ,content: $('#updateDeptForm')
                        ,area: ["500px","500px"]
                    });

                    //回显用户信息
                    form.val('updateDeptFilter',{
                        "deptId": data.deptId
                        ,"deptName": data.deptName
                        ,"createBy": data.createBy
                        ,"createTime": data.createTime
                        ,"updateBy": data.updateBy
                        ,"updateTime": data.updateTime
                    })
                    // 回显状态开关
                    if (data.state == "异常"){
                        $("#updateDeptForm input[type=checkbox]").prop("checked", "");
                    } else {
                        $("#updateDeptForm input[type=checkbox]").prop("checked", "checked");
                    };

                    //日期
                    laydate.render({
                        elem: '#date1'
                    });
                    laydate.render({
                        elem: '#date2'
                    });
                    // 更新用户信息
                    form.on('submit(updateDept)',function (data) {
                        $.ajax({
                            url: "${pageContext.request.contextPath}/dept/updateDept.do"
                            ,data: data.field
                            ,dataType: "json"
                            ,type: "post"
                            ,success: function (redata) {
                                if (redata.code == 0) {
                                    layer.closeAll();//关闭对话框
                                    layer.msg(redata.msg);
                                    // 改变状态开关
                                    if (data.state == "异常"){
                                        $("#updateDeptForm input[type=checkbox]").prop("checked", "");
                                    } else {
                                        $("#updateDeptForm input[type=checkbox]").prop("checked", "checked");
                                    }
                                    table.reload('demo',{
                                        page: {
                                            curr: 1
                                        }
                                    });
                                } else {
                                    layer.msg(redata.msg);
                                }
                            }
                        });
                        return false;
                    });
                } else if (layEvent === 'delete') {
                    layer.confirm('真的删除吗？',function (index){
                        $.ajax({
                            url: "${pageContext.request.contextPath}/dept/deleteDept.do"
                            ,data: {deptId: data.deptId}
                            ,dataType: "json"
                            ,type: "post"
                            ,success: function (redata) {
                                if (redata.code == 0) {
                                    layer.msg(redata.msg);
                                    layer.closeAll();//关闭对话框
                                    table.reload('demo',{
                                        page: {
                                            curr: 1
                                        }
                                    });
                                } else {
                                    layer.msg(redata.msg);
                                }
                            }
                        });
                    })
                }
            });
        });
    </script>
</html>
